<template>
  <div>
    <!-- 新能源sap信息维护 -->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
      @resetChange="resetChange"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button  size="small" type="primary" @click="addHandle(1)" >导出</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher :rowData="formData" @close="ownercloseHandler" />
    </BaseDialog>
    <Drawer
      :visible.sync="emitvisible"
      :title="'编辑'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
      width="72%"
    >
      <emitModel  :rowData="formData" @close="emitcloseHandler" />
    </Drawer>

  </div>
</template>
<script>
import {getList,exportList,getDeptList} from '@/api/cms-api/origin/sapDetail/index.js';
import { dictionary } from '@/utils/dictMixin';
import {exportExcelFile} from '@/utils'
import emitModel from './emitModel';
import ownerSeacher from './ownerSeacher';
export default {
  name:'TemplateDistribution',
  data(){
    this.BaseTable=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{
        returnStatus:60551002
      },
      fetchApi:getList,
      formData:{},
      visible:false,
      emitvisible:false,
      addvisible:false,
      issearch:false,
      dealerCode:'',
      selectList:[]
    }
  },
  mixins:[dictionary],
  components: {
    emitModel,
    ownerSeacher,
  },
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created(){
    this.getDeptList()
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'SELECT',
          label: '经销商类型',
          fieldName: 'dealerType',
          placeholder: '请选择',
          itemList:this.createDictList('2538')
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerCode',
          placeholder: '请输入',
          // readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change:p=>{
            if(!p){
              this.dealerCode=''
            }
          }
        },
        {
          type: 'SELECT',
          label: '经销商级别',
          fieldName: 'shopLevel',
          placeholder: '请选择',
          itemList:this.createDictList('2540')
        },
        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createdAtStartDate|createdAtEndDate',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '经营状态',
          fieldName: 'newenergyDealerStatus',
          placeholder: '请选择',
          itemList:this.createDictList('2552')
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          itemList:this.createRegionsList(),
          change:p=>{
            this.topFilterList.map(x=>{
              if(x.fieldName=='cityId'){
                x.initialValue='';
                x.itemList=p?this.createcityList(p):[]
              }
              if(x.fieldName=='districtId'){
                x.initialValue='';
                x.itemList=[]
              }
            })
          }
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          itemList:[],
          change:p=>{
            this.topFilterList.map(x=>{
              if(x.fieldName=='districtId'){
                x.initialValue='';
                x.itemList=p?this.district(p):[]
              }
            })
          }
        },
        {
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          itemList:[],
        },
        {
          type: 'SELECT',
          label: '是否已发送SAP',
          fieldName: 'isSapSend',
          placeholder: '请选择',
          itemList:this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: 'SAP经销商代码',
          fieldName: 'sapCode',
          placeholder: '请输入',
        },

      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '公司全称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'SAP经销商代码',
          dataIndex: 'sapCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商简称',
          dataIndex: 'dealerShortname',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商类型',
          dataIndex: 'dealerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2538'),
          render: (p) => {
            return <span >{this.createDictText(p.row.dealerType, '2538')}</span>;
          },
        },
        {
          title: '经销商级别',
          dataIndex: 'shopLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2540'),
          render: (p) => {
            return <span >{this.createDictText(p.row.shopLevel, '2540')}</span>;
          },
        },

        {
          title: '店面级别',
          dataIndex: 'constructLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2554'),
          render: (p) => {
            return <span >{this.createDictText(p.row.constructLevel, '2554')}</span>;
          },
        },
        {
          title: '展厅级别',
          dataIndex: 'saleLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2542'),
          render: (p) => {
            return <span >{this.createDictText(p.row.saleLevel, '2542')}</span>;
          },
        },
        {
          title: '售后级别',
          dataIndex: 'serviceLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2543'),
          render: (p) => {
            return <span >{this.createDictText(p.row.serviceLevel, '2543')}</span>;
          },
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '省份',
          dataIndex: 'provinceId',
          sorter: true,
          editItems:this.createRegionsList()
        },
        {
          title: '城市',
          dataIndex: 'cityId',
          sorter: true,
          render:(p)=>{
            let str=''
            if(p.row.provinceId&&p.row.cityId){
              let arr=this.createcityList(p.row.provinceId)
              arr.map(x=>
                {if(x.value==p.row.cityId){
                  str=x.text
                }}
              )
            }
            return <span>{str}</span>
          }
        },
        {
          title: '县区',
          dataIndex: 'districtId',
          sorter: true,
          render:(p)=>{
            let str=''
            if(p.row.cityId&&p.row.districtId){
              let arr=this.district(p.row.cityId)
              arr.map(x=>
                {if(x.value==p.row.districtId){
                  str=x.text
                }}
              )
            }
            return <span>{str}</span>
          }
        },
        {
          title: '系统管理员',
          dataIndex: 'systemManager',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '管理员电话',
          dataIndex: 'sysManagerPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '保证金金额',
          dataIndex: 'cashDeposit',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '启用标记',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: (p) => {
            return <span >{this.createDictText(p.row.status, '1003')}</span>;
          },
        },
        {
          title: '经营状态',
          dataIndex: 'dealerStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2552'),
          render: (p) => {
            return <span >{this.createDictText(p.row.dealerStatus, '2552')}</span>;
          },
        },
        {
          title: '是否已发送SAP',
          dataIndex: 'isSapSend',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span >{this.createDictText(p.row.isSapSend, '1004')}</span>;
          },
        },

      ]
    },
    selectChangeHandle(row){

    },
    addHandle(type){
    this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
          confirmButtonText: '下载',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let json=this.fetchParams;
          exportList(json).then( response => {
            exportExcelFile(response.data,'新能源SAP信息维护');
          }).catch( err => {
            console.log(err)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });

    },
    async getDeptList(){
      let res=await getDeptList({orgType:15061005})
      let arr=res.data.map(x=>({text:x.orgName,value:x.orgId}))
      this.topFilterList.find(x=>x.fieldName=='deptId').itemList=arr;
    },
    changeHandle(params){
      this.fetchParams.dealerCode=this.dealerCode;
      this.fetchParams=Object.assign({},this.fetchParams,params)
    },
    seacherFun(){
        this.issearch=true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data){
      this.formData=data;
      this.emitvisible=true
    },
    emitcloseHandler(val){
      if(val){
        this.fetchParams.dealerCode=this.dealerCode;
        this.fetchParams=Object.assign({},this.fetchParams)
      }
      this.emitvisible=false
    },
    ownercloseHandler(val){
      if(val){
        console.log(val)
        this.topFilterList.map(x=>{
          if(x.fieldName=='dealerName'){
            x.initialValue=val.dealerName;
            this.dealerCode=val.dealerCode
          }
        })
      }
      this.issearch=false;
    },
    resetChange(){
      this.dealerCode='';
      this.fetchParams.dealerCode='';
      this.fetchParams=Object.assign({},this.fetchParams)
    }
  }
}
</script>
